﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DocumentTrackingSystem.Service;
using DocumentTrackingSystem.Models;
using MvcPaging;
namespace DocumentTrackingSystem.Areas.admin.Controllers
{
    public class OfficeController : Controller
    {
        //
        // GET: /admin/Office/
        DTSEntities db = new DTSEntities();
        private readonly IOfficeServices _officeServices;
        public OfficeController()
        {
            _officeServices = new OfficeServices();
        }
        public ActionResult Index(int? page, string sortby)
        {
            var currentPageIndex = page.HasValue ? page.Value - 1 : 0;
            var offices=_officeServices.CustomerListWithPagingAndSorting(currentPageIndex, sortby, 10);
            Session["adminOfficePage"] = offices.PageNumber; 
            Session["adminOfficeSort"] = sortby??"";
            return View(offices);
            
        }
        public ActionResult CreateOffice()
        {
            var office = new Office();
            return PartialView(office);
        }
        [HttpPost]
        public ActionResult CreateOffice(Office model)
        {
          
                Office _office = new Office();    
                _officeServices.InsertOffice(model,out _office);
                var offices = _officeServices.CustomerListWithPagingAndSorting(((int)Session["adminOfficePage"])-1, Session["adminOfficeSort"].ToString(), 10);
              
            return PartialView("_Index", offices);
           
        }
        public ActionResult EditOffice(long id)
        {
            var office = _officeServices.Find(x => x.ID == id);
            return PartialView(office);
        }
        [HttpPost]
        public ActionResult EditOffice(Office model)
        {

            Office _office = new Office();
            _officeServices.FindAndUpdateOffice(model);
            var offices = _officeServices.CustomerListWithPagingAndSorting(((int)Session["adminOfficePage"]) - 1, Session["adminOfficeSort"].ToString(), 10);
         
            return PartialView("_Index", offices);

        }

    }
}
